# !/usr/bin/env python
# -*- coding:utf-8 -*-
from modelDb.baseModel import baseModel
import pymysql

class touristsInfoModel(baseModel):
    def __init__(self):
        super(touristsInfoModel, self).__init__()
        self._table = "tourists_info"

    # ====================================================
    # 添加客源
    # ====================================================
    def add(self, data):
        cursor = self.conn.cursor()
        sql = """
            insert into tourists_info(
                phone,cityarea_id,cityarea_name, business_area,business_area_ids,
                district, district_ids, house_room,house_hall,house_toilet,
                price,  area, page_type, appName, city
            ) values(
                '%(phone)s', %(cityarea_id)s,'%(cityarea_name)s','%(cityarea2_id)s','%(cityarea2_name)s',
                '%(borough_name)s','%(borough_id)s', %(house_room)s, '%(house_hall)s',%(house_toilet)s,
                %(house_price)s, %(house_totalarea)s, '%(page_type)s', '%(appName)s', '%(city)s'
            )""" % data
        print sql
        # exit()
        cursor.execute(sql)
        new_id = cursor.lastrowid

        self.conn.commit()
        cursor.close()
        return new_id



    #====================================================
    # 通过手机查找客源数据
    #====================================================
    def findByPhone(self, phone):
        cursor = self.conn.cursor(cursor=pymysql.cursors.DictCursor)

        sql = "select * from %(table)s where phone = '%(phone)s'" % {"table":self._table, "phone":phone}
        cursor.execute(sql)
        result = cursor.fetchone()

        cursor.close()
        return result

    #====================================================
    # 跟新etl清洗的数据
    #====================================================
    def updateEtl(self, id, data_status):
        cursor = self.conn.cursor()

        sql = "update tourists_actions set data_status=%d where user_id=%d" % (id, data_status)
        effect_row = cursor.execute(sql)
        self.conn.commit()

        # 关闭游标
        cursor.close()
        return effect_row


    #====================================================
    # 获取etl清洗的数据
    #----------------------------------------------------
    # phone 手机号
    # source_data 原数据
    # data 更新的数据
    #====================================================

    def saveImport(self, phone, source_data, data):
        print source_data
        print data
        #如果小区id相同，数据不修改
        if source_data.get("borough_id") == data.get("borough_id"):return True
        data.setdefault("tourists_info_id", source_data.get("id"))

        cursor = self.conn.cursor()
        sql = """
            update tourists_info set
                cityarea_id=%(cityarea_id)s,  cityarea_name='%(cityarea_name)s', business_area = '%(cityarea2_name)s',business_area_ids= '%(cityarea2_id)s',
                district='%(borough_name)s', district_ids='%(borough_id)s', house_room=%(house_room)s,house_hall='%(house_hall)s',house_toilet=%(house_toilet)s,
                price=%(house_price)s,  area=%(house_totalarea)s, page_type='%(page_type)s', appName='%(appName)s', city='%(city)s'
            where id = '%(tourists_info_id)s'
            """ % data
        print sql
        effect_row = cursor.execute(sql)
        self.conn.commit()

        # 关闭游标
        cursor.close()
        return effect_row